package com.cat.binaryTree;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/delete-nodes-and-return-forest/description/https://leetcode.cn/problems/delete-nodes-and-return-forest/description/
 * @create 2025/9/14 15:22
 * @since JDK17
 */

public class Solution34 {
    List<TreeNode> list = new ArrayList<>();
    Set<Integer> set = new HashSet<>();
    TreeNode f(TreeNode root) {
        if (root == null) {
            return null;
        }
        root.left = f(root.left);
        root.right = f(root.right);
        if (set.contains(root.val)) {
            if (root.left != null) {
                list.add(root.left);
            }
            if (root.right != null) {
                list.add(root.right);
            }
            return null;
        }
        return root;
    }
    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        for (int num : to_delete) {
            set.add(num);
        }
        f(root);
        if (!set.contains(root.val)) {
            list.add(root);
        }
        return list;
    }
}
